package com.ljkj.blockpay.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.kongzue.baseokhttp.util.JsonBean;
import com.kongzue.baseokhttp.util.JsonList;
import com.ljkj.blockpay.R;
import com.ljkj.blockpay.basedata.BaseActivity;
import com.ljkj.blockpay.databinding.ActivityHistorytradingBinding;
import com.ljkj.blockpay.model.TimeModel;
import com.ljkj.blockpay.presenter.HistoryPresenter;
import com.ljkj.blockpay.presenter.inter.HistoryInter;
import com.ljkj.blockpay.ui.adapter.HistoryAdapter;
import com.ljkj.blockpay.util.ToastUtils;
import com.ljkj.blockpay.widget.ClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 交易历史界面
 */
public class HistoryTradingActivity extends BaseActivity<ActivityHistorytradingBinding, HistoryPresenter> implements HistoryInter.HistoryInterView,ClickListener {

    private HistoryAdapter historyAdapter;



    @Override
    public int initContentView() {
        return R.layout.activity_historytrading;
    }

    @Override
    protected HistoryPresenter createPresenter() {
        return new HistoryPresenter(this,this);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        binding.tvTitle.setLeftBackFinish(this);
        binding.tvTitle.setTitleContent(R.string.history);
        getPresenter().pageNum = 1;
        getPresenter().getlist();
    }

    @Override
    protected void initData() {
        super.initData();
        //初始化一个list的数据
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rvHistorylist.setLayoutManager(layoutManager);
        historyAdapter = new HistoryAdapter(R.layout.historyhead_list,listTimeModel);
        binding.rvHistorylist.setAdapter(historyAdapter);
        historyAdapter.addChildClickViewIds(R.id.image_up);
        historyAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.image_up){
                List<TimeModel> data = historyAdapter.getData();
                data.get(position).setSwitchBoolean(!data.get(position).isSwitchBoolean());
                historyAdapter.notifyDataSetChanged();
            }
        });
        binding.smart.setOnRefreshListener((vlaue) -> {
            getPresenter().pageNum = 1;
            getPresenter().getlist();
        });
        binding.smart.setOnLoadMoreListener((value) ->{
            getPresenter().pageNum ++;
            getPresenter().getlist();
        });
        historyAdapter.setSetAdpItemChickListener((position,itemPosition) -> {
            TimeModel timeModel = listTimeModel.get(position);
            List<TimeModel.DataTimeModel> list = timeModel.getList();
            TimeModel.DataTimeModel dataTimeModel = list.get(itemPosition);
            Log.d("zdl","===="+dataTimeModel.toString());
            Log.d("zdl","====="+position+"\n"+itemPosition);
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){


        }
    }
    List<TimeModel> listTimeModel = new ArrayList<>();

    @Override
    public void historylist(JsonList jsonList) {
        binding.smart.finishRefresh();
        binding.smart.finishLoadMore();

        if (getPresenter().pageNum == 1){
            List<TimeModel> timeModeList = getPresenter().getTimeModeList(jsonList);
            binding.textGone.setVisibility(timeModeList.size() == 0?View.VISIBLE:View.GONE);
            listTimeModel.clear();
            listTimeModel.addAll(timeModeList);

        }else{
            List<TimeModel> timeModeList = getPresenter().getTimeModeList(jsonList);
            //1首先 计算是否有重复的时间
            //2 将不是重复的添加到上一个集合中
            if (timeModeList.size() == 0){
                Toast.makeText(baseContext, "没有更多数据了", Toast.LENGTH_SHORT).show();
                return;
            }else{
                for (int i = 0; i < listTimeModel.size(); i++) {
                    for (int j = 0; j < timeModeList.size(); j++) {
                        if (listTimeModel.get(i).getYearDay().equals(timeModeList.get(j).getYearDay())){
                            listTimeModel.get(i).addData(timeModeList.get(j).getList());
                            timeModeList.remove(j);
                        }
                    }
                }
                listTimeModel.addAll(timeModeList);
            }
        }
        historyAdapter.notifyDataSetChanged();

    }
}